package com.group4.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;

public class Tester {

	public static void main(String[] args) {

		if (args.length < 1)
			System.err.println("Method name required");

		String ID = "";
		if (args[1] != null && !args[1].equalsIgnoreCase("")) {
			ID = args[1];
		}else{
			System.err.println("ID value is required");
		}

		String xml = "";
		if (args[0].equalsIgnoreCase("getVideoWithID"))
			xml = "<operation> <method name=\"getVideoWithID\"> <ID>" + ID
					+ "</ID> </method> </operation>";
		else if (args[0].equalsIgnoreCase("addVideo"))
			xml = "<operation> <method name=\"addVideo\"> <ID>"
					+ ID
					+ "</ID> <video_name>Test Video</video_name> <video_uploaderId>1</video_uploaderId> <video_url>Test Url</video_url> <video_uploadDateTime>2014-11-05</video_uploadDateTime> <video_uploadLocation>c:\\videos</video_uploadLocation><video_description>Test</video_description><video_rate>1</video_rate><video_tag>Rock</video_tag></method></operation>";
		else if (args[0].equalsIgnoreCase("updateVideo"))
			xml = "<operation> <method name=\"updateVideo\"> <ID>"
					+ ID
					+ "</ID> <video_name>Updated new name</video_name> <video_uploaderId>1</video_uploaderId> <video_url>Test Url</video_url> <video_uploadDateTime>2014-11-05</video_uploadDateTime> <video_uploadLocation>c:\\videos</video_uploadLocation><video_description>Test</video_description><video_rate>1</video_rate><video_tag>Pop</video_tag></method></operation>";
		else if (args[0].equalsIgnoreCase("deleteVideo"))
			xml = "<operation> <method name=\"deleteVideo\"> <ID>" + ID
					+ "</ID> </method> </operation>";
		else {
			System.err.println("Invalid method name");
		}
		sendRequestToCmpe(xml);
	}

	public static String sendRequestToCmpe(String xml) {
		String result = "";
		String SMSResult = "1";
		PrintWriter writer = null;
		BufferedReader reader = null;
		HttpURLConnection Connection = ConnectToCmpe("http://127.0.0.1:8080/test");
		if (Connection != null) {
			Connection.setDoOutput(true);
			Connection.setDoInput(true);
			Connection.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			Connection.setRequestProperty("Content-Type",
					"test/html;charset=\"UTF-8\"");
			Connection.setUseCaches(false);
			try {
				writer = new PrintWriter(Connection.getOutputStream());
				writer.println(xml);
				writer.flush();
				Charset cs = Charset.forName("utf-8");
				reader = new BufferedReader(new InputStreamReader(
						Connection.getInputStream(), cs));
				result = reader.readLine();
				System.out.println("Result : " + result);
				// writeFile(result);
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
		}
		return SMSResult;
	}

	public static HttpURLConnection ConnectToCmpe(String URLAddress) {
		URL url = null;
		java.net.URLConnection Connection = null;
		HttpURLConnection huc = null;
		try {
			url = new URL(URLAddress);
			Connection = url.openConnection();
			huc = (HttpURLConnection) Connection;
		} catch (MalformedURLException mfURLe) {
			System.out.println(mfURLe.getMessage());
		} catch (IOException ioe) {
			System.out.println(ioe.getMessage());
		}
		return huc;
	}

}
